WAFER_DIR = ../..
WAFER_BUILD_DIR = $(WAFER_DIR)/build
WAFER_OPT = $(WAFER_BUILD_DIR)/bin/wafer-opt
WAFER_TRANSLATE = $(WAFER_BUILD_DIR)/bin/wafer-translate
CLANG = $(WAFER_DIR)/3rdparty/llvm/build/bin/clang++

all: md5 md5Crack

md5Crack: crackCaller.cpp md5.o
	$(CLANG) $^ -g -static \
	-L$(WAFER_BUILD_DIR)/lib/FPGAAPI -lwafer_fpga_api \
	-o $@


md5: md5.o caller.cpp
	$(CLANG) $^ -g -static \
	-L$(WAFER_BUILD_DIR)/lib/FPGAAPI -lwafer_fpga_api \
	-o $@

md5.o: md5.ll
	$(CLANG) -c $< -o $@

md5.ll: md5.mlir
	$(WAFER_OPT) $< --lower-crypto-to-fpga | \
	$(WAFER_OPT) -finalize-memref-to-llvm | \
	$(WAFER_OPT) -convert-func-to-llvm | \
	$(WAFER_OPT) -reconcile-unrealized-casts | \
	$(WAFER_TRANSLATE) --wafer-to-llvmir -o $@

.PHONY: clean run

run:
	./md5
	./md5Crack

clean:
	rm -rf *.o
	rm -rf *.out
	rm -rf *.ll
	rm -rf *.s
	rm md5
	rm md5Crack
